//
//  BlipEchoViewController.m
//  BlipEcho
//
//  Created by James Tuley on 9/14/08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//

#import "BlipEchoViewController.h"
#import <Blip/BLIPConnection.h>
#import <Blip/BLIPRequest.h>
#import <Blip/Target.h>

@interface BlipEchoViewController(Private)
- (void)openConnection: (NSNetService*)aService;
/* Closes the currently open BLIP connection. */
- (void)closeConnection;
@end

@implementation BlipEchoViewController

@synthesize service;




// Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillDisappear:(BOOL)animated {
	[self closeConnection];
}

/* Opens a BLIP connection to the given address. */
- (void)openConnection: (NSNetService*)aService 
{
    _connection = [[BLIPConnection alloc] initToNetService:aService];
    if( _connection )
        [_connection open];
    else
        NSLog(@"Error opening connection");
}

/* Closes the currently open BLIP connection. */
- (void)closeConnection
{
    [_connection close];
    [_connection release];
    _connection = nil;
}



#pragma mark UITextView delegate methods

- (void)textViewDidBeginEditing:(UITextView *)textView
{
	// provide my own Save button to dismiss the keyboard
	UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
																			  target:self action:@selector(saveAction:)];
	self.navigationItem.rightBarButtonItem = saveItem;
	[saveItem release];
}

- (void)saveAction:(id)sender
{

	if(_connection ==nil)
		[self openConnection:service];
	
	BLIPRequest *r = [_connection request];
    r.bodyString = [request text];
    BLIPResponse *tResponse = [r send];
    tResponse.onComplete = $target(self,gotResponse:);
	
	[request resignFirstResponder];
	self.navigationItem.rightBarButtonItem = nil;	// this will remove the "save" button
}

/* Receive the response to the BLIP request, and put its contents into the response field */
- (void) gotResponse: (BLIPResponse*)aResponse
{
    [ response setText: aResponse.bodyString];
}    

/*
// Implement loadView to create a view hierarchy programmatically.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}

@end
